const baseController = require("./baseController");
const userModel = require("../../model/userModel");
const encAndDec = require("../../untis/encAndDec");

class userController extends baseController {
    async login() {
        if(this.req.session) {
            let userInfo = JSON.parse(this.req.session);
            return '301';
        } else {
            return this.display("login.html");
        }
    }
    //前台用户注册页面
    async userReg() {
        return this.display("userReg.html");
    }
    //后端逻辑用户注册
    async handleReg() {
        let name = this.req.post['username'];
        let password = this.req.post['password'];
        let sql = "select * from user where name=? and password=? limit 1";
        let usermodel = new userModel();
        let userList = await usermodel.query(sql,[name,password]);   
        console.log(userList);
        // let password = encAndDec.md5(this.req.post['password']);
        // console.log(this.req.    post); //获取用户名和密码数据  //加解密 crypto模板
        // return this.display("userReg.html");
        //完善-------->写入到 数据库
        let userInfo = {};
        if(userList.length > 0) {   //长度大于0，说明用户名密码都是正确的
            userInfo = userList[0];
            this.res.session = JSON.stringify(userInfo);
        }
        return JSON.stringify(userInfo);
    }

    async userList() {
        let user = new userModel();
        try {
            let userList = await user.select();
            return '我是用户列表页面' //就是把对象转成json格式
        } catch (err) {
            console.log(err);
        }
    }
}
module.exports = userController;